package com.zsk.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsk.entity.RoleEntity;
import com.zsk.mapper.RoleEntityMapper;
import com.zsk.service.IRoleEntityService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author keke
 * @since 2020-07-23
 */
@RestController
@RequestMapping("/role-entity")
public class RoleEntityController {

    @Autowired
    IRoleEntityService roleService;
    @Autowired
    RoleEntityMapper roleMapper;

    @GetMapping("/roles")
    public List<RoleEntity> getRoles() {
        return roleService.list();
    }

    @GetMapping("/role/{name}")
    public List<RoleEntity> getRole(@PathVariable String name) {
        QueryWrapper<RoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name", name);
        return roleService.list(wrapper);
    }

    @GetMapping("/roles/{pageIndex}/{pageSize}")
    public IPage<RoleEntity> getRoles(@PathVariable int pageIndex, @PathVariable int pageSize) {
        IPage<RoleEntity> page = new Page<>();
        page.setCurrent(pageIndex);
        page.setSize(pageSize);
        return roleService.page(page);
    }
    
    @GetMapping("/roles/{userName}")
    public List<RoleEntity> getRoles(@PathVariable String userName) {
        return roleMapper.listUserRoles(userName);
    }

}
